﻿using System.Drawing;
using System.Drawing.Drawing2D;
using FBEntities.FBObjects;
using XFControls;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;

namespace XFItemControls.XFPanels.XFPanelItems
{
    public class XFItemPlace : XFItemBase
    {
        private Font _nameFont;
        private int _height;
        public FBPlace Place { get; set; }
        private int _lineSpace = 5;

        public XFItemPlace(FBPlace place)
        {
            Place = place;

            Style = new XFItemStyle
            {
                BackgroundColor = Color.White,
                SelectedBackgroundColor = Color.Lavender,
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                TextColor = Color.Black,
                SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Underline),
                SecondaryTextColor = Color.Navy,
                DashStyleBottom = DashStyle.Solid,
                BorderBottomColor = Color.DarkGray,
                Padding = 5
            };

            _nameFont = new Font(Style.TextFont.Name, Style.TextFont.Size, FontStyle.Bold);
            ItemType = XFItemType.Clickable;
        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (Place == null)
                return Style.PaddingTop + Style.PaddingBottom;

            if (_height > 0)
                return _height;

            int width = GetWidth();
            int currX = Style.PaddingLeft + Style.PaddingRight;
            int currY = Style.PaddingTop;

            int currWidth = width - (currX + Style.PaddingRight + Style.PaddingLeft);

            mesText(Place.name, ref currWidth, ref currY, _nameFont);

            if (Place.location != null)
            {
                string local = GetAddress(Place.location);
                mesText(local, ref currWidth, ref currY, Style.TextFont);
            }

            currY += _lineSpace;

            _height = currY + Style.PaddingBottom;

            return _height;
        }

        private string GetAddress(FBLocation loc)
        {
            string local = loc.street;
            if (!string.IsNullOrEmpty(local))
                local += "\n";

            if (!string.IsNullOrEmpty(loc.city) && !string.IsNullOrEmpty(loc.state) && !string.IsNullOrEmpty(loc.zip))
                local += loc.city + ", " + loc.state + " " + loc.zip;

            if (!string.IsNullOrEmpty(loc.country))
                local += "\n" + loc.country;
            return local;
        }

        private void mesText(string text, ref int currWidth, ref int currY, Font f)
        {
            if (!string.IsNullOrEmpty(text))
            {
                int h = f.MeasureString(text, new Rectangle(0, 0, currWidth, 0)).Height + _lineSpace;
                currY += h;
            }
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            if (Place == null)
                return;

            base.ItemPaint(g, x, y);

            int width = GetWidth();
            int currX = Style.PaddingLeft + Style.PaddingRight;
            int currY = Style.PaddingTop;

            int currWidth = width - (currX + Style.PaddingRight + Style.PaddingLeft);

            Color textColor = getCurrentFontColor(false);
            Color secondaryTextColor = getCurrentFontColor(false);

            drawText(g, Place.name, ref currX, ref currWidth, ref currY, _nameFont,
                     secondaryTextColor);

            if (Place.location != null)
            {
                string local = GetAddress(Place.location);
                drawText(g, local, ref currX, ref currWidth, ref currY, Style.TextFont,
                         textColor);
            }
        }

        public override XFItemClickResult GetClickResult()
        {
            if (Parent == null)
                return XFItemClickResult.Nothing;

            XFPanelList rp = XFItemControlUtils.SetupCheckin(Place);
            rp.PanelName = Place.name;

            var back = new XFItemBackHeader();

            rp.AddHeaderItem(back);
            Parent.SetRightPanel(rp);

            rp.ShowAlphaBar(false);
            rp.ShowScrollbar(true);

            return XFItemClickResult.SlideLeft;
        }

        private void drawText(Graphics g, string text, ref int currX, ref int currWidth, ref int currY, Font font, Color color)
        {
            if (!string.IsNullOrEmpty(text))
            {
                int h = g.DrawString(text, new Rectangle(currX, currY, currWidth, 0), Style, font, color) +
                        _lineSpace;
                currY += h;
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            _nameFont.Dispose();
        }
    }
}
